﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text.RegularExpressions;

using Telerik.Web.UI;

using ConstructionBidOnline.Biz;
using ConstructionBidOnline.Common;

public partial class ManageMembershipSettings : System.Web.UI.Page
{
    #region Constants

    private const string PROP_DATASRC_MEMBERSHIPSETTINGS = "__MEMBERSHIPSETTINGS::MEMBERSHIPSETTINGS";


    #endregion

    #region Public Properties

    public MembershipSettingsCollection DataSourceMembershipSettings
    {
        get { return (SessionMgr.Session[PROP_DATASRC_MEMBERSHIPSETTINGS] is MembershipSettingsCollection ? (MembershipSettingsCollection)SessionMgr.Session[PROP_DATASRC_MEMBERSHIPSETTINGS] : (MembershipSettingsCollection)(SessionMgr.Session[PROP_DATASRC_MEMBERSHIPSETTINGS] = new MembershipSettingsCollection())); }
        set { SessionMgr.Session[PROP_DATASRC_MEMBERSHIPSETTINGS] = value; }
    }

    public MembershipSettings msobj;
    public MembershipSettings msobjmembershiptype;


    #endregion    

    #region Event Handlers

    #region Page Load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            //InitMaxFieldLengths();

        }
    }
    #endregion

    #region Button submit click

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        if (Page.IsValid)
        {
            //Database entry 
             msobjmembershiptype = MembershipSettings.GetMembershipSettingsByMembershipType(comboMembershipType.Text);
            if (msobjmembershiptype != null)
            {
                //if (comboMembershipType.Text == msobj1.MembershipType)
                //{
                UpdateData(UpdateDirection.UI_To_Obj);
                MembershipSettings.Update(msobjmembershiptype);
                //}
                lblmsgs.Visible = true;
                //lblmsgs.BackColor ="Red";
                lblmsgs.Text = "Your Record has been updated sucessfully";
            }
            else
            {
                msobj = MembershipSettings.CreateNew();
                UpdateData(UpdateDirection.UI_To_Obj);
                MembershipSettings.Update(msobj);
                lblmsgs.Visible = true;
                //lblmsgs.BackColor ="Red";
                lblmsgs.Text = "Your Record has been submitted sucessfully";
            }
            
        }
    }

    #endregion

    #region Button cancel click

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("DashBoard.aspx");
    }

    #endregion

    #region  ComboMembershipType_SelectedIndexChanged

    protected void comboMembershipType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
         msobjmembershiptype = MembershipSettings.GetMembershipSettingsByMembershipType(comboMembershipType.Text);
         if (msobjmembershiptype != null)
         {
             comboMemberType.SelectedValue = msobjmembershiptype.MemberType;
             if (msobjmembershiptype.MemberType == "Consultant")
             {
                 divforconsultantOnly.Visible = true;
                 divforcontractorOnly.Visible = false;
                 txtfeaturedjobs.Text = msobjmembershiptype.NumberOfFeaturedJobs;
                 txtNonfeaturedjobs.Text = msobjmembershiptype.NumberOfNonFeaturedJobs;
             }
             if (msobjmembershiptype.MemberType == "Contractor")
             {
                 divforcontractorOnly.Visible = true;
                 divforconsultantOnly.Visible = false;
                 txtNoOfbids.Text = msobjmembershiptype.NumberOfBids;
             }
             string validupto = msobjmembershiptype.ValidUpTo;
             string[] lines = Regex.Split(validupto, ",");
             
             txtvalidupto.Text = lines[0].ToString();
             comboTime.SelectedValue = lines[1].ToString();

             txtmembershipFees.Text = msobjmembershiptype.MembershipFees;
             txtNoOfkeywords.Text = msobjmembershiptype.NumberOfKeywords;
             lblmsgs.Visible = false;

         }
         else
         {
             //textboxes to be kept blank and corresponding div invisible
             divforcontractorOnly.Visible = false;
             divforconsultantOnly.Visible = false;
             comboMemberType.SelectedValue = "";
             txtfeaturedjobs.Text = "";
             txtNonfeaturedjobs.Text = "";
             txtNoOfbids.Text = "";
             txtvalidupto.Text = "";
             comboTime.SelectedValue = "";
             txtmembershipFees.Text = "";
             txtNoOfkeywords.Text = "";
             lblmsgs.Visible = true;
             lblmsgs.Text = "No Records Found";
         }
    }
    #endregion

    #region  ComboMemberType_SelectedIndexChanged

    protected void comboMemberType_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
    {
        if (comboMemberType.Text == "Consultant")
        {
            divforconsultantOnly.Visible = true;
            divforcontractorOnly.Visible = false;
        }
        
        if (comboMemberType.Text == "Contractor")
        {
            divforcontractorOnly.Visible = true;
            divforconsultantOnly.Visible = false;
        }
        if (comboMemberType.Text == "Select.")
        {
            divforcontractorOnly.Visible = false;
            divforconsultantOnly.Visible = false;
        }
    }

    #endregion

    #endregion

    #region Privates

    #region UpdateData

    public void UpdateData(UpdateDirection updatedir)
    {

        if (updatedir == UpdateDirection.Obj_To_UI)
        {
            //View Elements





            //Edit Elements
            

        }

        else
        {
             msobjmembershiptype = MembershipSettings.GetMembershipSettingsByMembershipType(comboMembershipType.Text);
             if (msobjmembershiptype != null)
            {
                msobjmembershiptype.MembershipFees = txtmembershipFees.Text.Trim();
                msobjmembershiptype.NumberOfKeywords = txtNoOfkeywords.Text.Trim();
                msobjmembershiptype.ValidUpTo = txtvalidupto.Text.Trim() +","+ comboTime.Text;
                //DataBase entry for Consultant
                if (comboMemberType.Text == "Consultant")
                {
                    msobjmembershiptype.MemberType = comboMemberType.Text;
                    msobjmembershiptype.NumberOfFeaturedJobs = txtfeaturedjobs.Text.Trim();
                    msobjmembershiptype.NumberOfNonFeaturedJobs = txtNonfeaturedjobs.Text.Trim();
                    msobjmembershiptype.NumberOfBids = null;
                }

                //DataBase entry for Contractor
                if (comboMemberType.Text == "Contractor")
                {
                    msobjmembershiptype.MemberType = comboMemberType.Text;
                    msobjmembershiptype.NumberOfBids = txtNoOfbids.Text.Trim();
                    msobjmembershiptype.NumberOfFeaturedJobs = null;
                    msobjmembershiptype.NumberOfNonFeaturedJobs = null;
                }
            }
            else
            {
                msobj.MembershipType = comboMembershipType.Text;
                msobj.MembershipFees = txtmembershipFees.Text.Trim();
                msobj.NumberOfKeywords = txtNoOfkeywords.Text.Trim();
                msobj.ValidUpTo = txtvalidupto.Text.Trim() + "," + comboTime.Text;
                //DataBase entry for Consultant
                if (comboMemberType.Text == "Consultant")
                {
                    msobj.MemberType = comboMemberType.Text;
                    msobj.NumberOfFeaturedJobs = txtfeaturedjobs.Text.Trim();
                    msobj.NumberOfNonFeaturedJobs = txtNonfeaturedjobs.Text.Trim();
                }

                //DataBase entry for Contractor
                if (comboMemberType.Text == "Contractor")
                {
                    msobj.MemberType = comboMemberType.Text;
                    msobj.NumberOfBids = txtNoOfbids.Text.Trim();
                }
            }
        }


    }

    #endregion

    #endregion

    
}
